# -*- coding: utf-8 -*-

# Scrapy settings for za_direct_crawler project
#
# For simplicity, this file contains only settings considered important or
# commonly used. You can find more settings consulting the documentation:
#
#     https://doc.scrapy.org/en/latest/topics/settings.html
#     https://doc.scrapy.org/en/latest/topics/downloader-middleware.html
#     https://doc.scrapy.org/en/latest/topics/spider-middleware.html
import os
from extra_apps.envs import deploy_env
from extra_apps.config.mongo_config import Mongo_CONF
from extra_apps.config.cache_config import REDIS_CONF
from extra_apps.get_md5 import get_md5

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
print('Run envs is：{}'.format(deploy_env))

# 日志配置
import time
import sys
import logging

if 'win' in sys.platform:
    log_dir = os.path.join(BASE_DIR, 'logs/{}'.format(int(time.time())))
else:
    log_dir = '/alidata1/admin/za_direct/logs/{}'.format(int(time.time()))

if not os.path.exists(log_dir):
    os.makedirs(log_dir)
LOG_ENABLED = False
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'standard': {
            'format': '%(asctime)s [%(threadName)s:%(thread)d] [%(name)s:%(lineno)d] [%(filename)s] [%(module)s:%(funcName)s] [%(levelname)s]- %(message)s'},
        "console": {
            'format': '[%(module)s] %(message)s'}

        # 日志格式
    },
    'filters': {
    },
    'handlers': {
        # 'mail_admins': {
        #     'level': 'ERROR',
        #     'class': 'django.utils.log.AdminEmailHandler',
        #     'include_html': True,
        # },
        'default': {
            'level': 'INFO',
            'class': 'logging.handlers.RotatingFileHandler',
            'encoding': 'utf-8',  # 编码
            'filename': '{}/all.log'.format(log_dir),  # 日志输出文件
            'maxBytes': 1024 * 1024 * 50,  # 文件大小
            'backupCount': 15,  # 备份份数
            'formatter': 'standard',  # 使用哪种formatters日志格式
            'delay': True,  # 设置 启动轮转
        },
        'error': {
            'level': 'WARNING',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': '{}/error.log'.format(log_dir),
            'encoding': 'utf-8',  # 编码
            'maxBytes': 1024 * 1024 * 50,
            'backupCount': 15,
            'formatter': 'standard',
            'delay': True,
        },
        'console': {
            'level': 'INFO',
            'class': 'logging.StreamHandler',
            'formatter': 'console'
        },
        'request_handler': {
            'level': 'DEBUG',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': '{}/script.log'.format(log_dir),
            'maxBytes': 1024 * 1024 * 50,
            'encoding': 'utf-8',  # 编码
            'backupCount': 15,
            'formatter': 'standard',
            'delay': True,
        },
        'scprits_handler': {
            'level': 'DEBUG',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': '{}/script.log'.format(log_dir),
            'maxBytes': 1024 * 1024 * 50,
            'backupCount': 15,
            'formatter': 'standard',
        }
    },
    'loggers': {
        'django': {
            'handlers': ['default', 'console'],
            'level': 'DEBUG',
            'propagate': False
        },
        'django.request': {
            'handlers': ['request_handler'],
            'level': 'DEBUG',
            'propagate': False,
        },
        'scripts': {
            'handlers': ['scprits_handler'],
            'level': 'DEBUG',
            'propagate': False
        },
        'sourceDns.webdns.views': {
            'handlers': ['default', 'error'],
            'level': 'DEBUG',
            'propagate': True
        },
        'sourceDns.webdns.util': {
            'handlers': ['error'],
            'level': 'ERROR',
            'propagate': True
        }
    },
    'root': {
        'handlers': ['default', 'console', 'error'],
        'level': 'INFO',
        'propagate': 0,
    }
}
from logging.config import dictConfig

dictConfig(LOGGING)

Log = logging.getLogger('app')


# Mongodb
mongo_conf = Mongo_CONF.get(deploy_env)